package com.moleq.device.printer;

import com.moleq.util.StringUtils;

public class PrinterBufferColumn {

	private String mContent = "";
	private double mScaleOfRow = 0;
	private double mWidth = 0;
	
	public int fontSize = Printer.FONT_SINGLE_SIZE;

	public int alignment = Printer.LEFT;
	private int type = Printer.CUSTOM_LINE;

	public void setScale(double scale) {
		if (scale < 0 || scale > 100) {
			scale = 100;
		}
		this.mScaleOfRow = scale;

	}
	
	public long getWidth(int totalWidth){
		mWidth = totalWidth * (mScaleOfRow / 100);
		return Math.round(mWidth) ;
	}

	// public void setFontSize(int size){
	// this.fontSize = size;
	// }

	public void setContent(String parm) {
		this.type = Printer.CUSTOM_LINE;
		this.setContent(parm, Printer.FONT_SINGLE_SIZE, Printer.LEFT);
	}

	public void setContent(String parm, int fontSize, int align) {

		this.type = Printer.CUSTOM_LINE;
		this.fontSize = fontSize;
		this.alignment = align;
		this.mContent = parm;

	}

	public void setContent(String parm, int type) {
		this.type = Printer.FULL_LINE;
		this.mContent = parm;
	}

	/*
	 * public String getContent(){ String resultContent = ""; String sizeStr =
	 * "";
	 * 
	 * // switch (fontSize) { // case Printer.FONT_SINGLE_SIZE: // sizeStr =
	 * ESC_BASE + SINGLE_SIZE; // break; // case Printer.FONT_DOUBLE_SIZE: //
	 * sizeStr = ESC_BASE + DOUBLE_SIZE; // break; // case
	 * Printer.FONT_DOUBLE_HEIGHT: // sizeStr = ESC_BASE + DOUBLE_HEIGHT; //
	 * break; // }
	 * 
	 * int iLen = mContent.length(); int i;
	 * 
	 * i = mMaxWidth * (mScaleOfRow/100);
	 * 
	 * i = i - iLen; switch(alignment){ case Printer.LEFT: resultContent =
	 * mContent; break; case Printer.CENTER: i = i/2; resultContent =
	 * StringUtils.fillStringOnLeft(mContent, i, " "); break; case
	 * Printer.RIGHT: resultContent = StringUtils.fillStringOnLeft(mContent, i,
	 * " "); break; }
	 * 
	 * return sizeStr+ resultContent; }
	 */

	public String getContent(int mWidth) {
		String resultContent = "";
		String sizeStr = "";

		// switch (fontSize) {
		// case Printer.FONT_SINGLE_SIZE:
		// sizeStr = ESC_BASE + SINGLE_SIZE;
		// break;
		// case Printer.FONT_DOUBLE_SIZE:
		// sizeStr = ESC_BASE + DOUBLE_SIZE;
		// break;
		// case Printer.FONT_DOUBLE_HEIGHT:
		// sizeStr = ESC_BASE + DOUBLE_HEIGHT;
		// break;
		// }

		int curntWidth;
//		double j;
//		j = mWidth * (mScaleOfRow / 100);
		curntWidth = (int) getWidth(mWidth);

		switch (type) {
		case Printer.CUSTOM_LINE:
			resultContent = getCustomContent(curntWidth);
			break;

		case Printer.FULL_LINE:
			resultContent = getFullContent(curntWidth);
			break;
		default:
			break;
		}

		return sizeStr + resultContent;
	}

	private String getFullContent(int width) {
		String result = "";
		String strEnd = "";
		int iLen = mContent.length();
		int count = width / iLen;
		int j = width % iLen;

		if (j > 0) {
			strEnd = mContent.substring(0, j);
		}

		for (int i = 0; i < count; i++) {
			result += mContent;
		}

		return result + strEnd;
	}

	private String getCustomContent(int width) {
		String resultContent = "";
		String sizeStr = "";
		int fillWidth = 0;
		int iLen = mContent.length();
		fillWidth = width - iLen;
		switch (alignment) {
		case Printer.LEFT:
			resultContent = StringUtils.fillStringOnRight(mContent, fillWidth, " ");
			break;
		case Printer.CENTER:
			fillWidth = fillWidth / 2;
			resultContent = StringUtils.fillStringOnLeft(mContent, fillWidth, " ");
			resultContent = StringUtils.fillStringOnRight(resultContent, fillWidth, " ");
			break;
		case Printer.RIGHT:
			resultContent = StringUtils.fillStringOnLeft(mContent, fillWidth, " ");
			break;
	
		}

		return sizeStr + resultContent;
	}

	public void setFontSize(int fontSize) {
		this.fontSize = fontSize;

	}

	public void setAlign(int align) {
		this.alignment = align;

	}
}
